package com.ld.security.shop.crawler.modules.wo.api;

import cn.hutool.core.util.ObjectUtil;
import com.ld.security.common.core.base.vo.DataVo;
import com.ld.security.common.core.base.vo.ListVo;
import com.ld.security.common.core.exception.ApiException;
import com.ld.security.common.core.util.*;
import com.ld.security.common.mybatis.base.po.IdPo;
import com.ld.security.common.mybatis.base.po.IdsPo;
import com.ld.security.common.tomcat.base.api.*;

import com.ld.security.common.tomcat.util.RespUtil;
import com.ld.security.shop.crawler.modules.wo.constant.WoGroupConstant;
import com.ld.security.shop.crawler.modules.wo.constant.WoTaskConstant;
import com.ld.security.shop.crawler.modules.wo.entity.WoData;
import com.ld.security.shop.crawler.modules.wo.entity.WoGroup;

import com.ld.security.shop.crawler.modules.wo.entity.WoTask;
import com.ld.security.shop.crawler.modules.wo.po.ExportDataPo;
import com.ld.security.shop.crawler.modules.wo.po.TaskDispatchPo;
import com.ld.security.shop.crawler.modules.wo.service.IWoDataService;
import com.ld.security.shop.crawler.modules.wo.service.IWoGroupService;
import com.ld.security.shop.crawler.modules.wo.service.IWoTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@Api(tags="任务组Api管理")
@RequestMapping("/api/woGroup")
public class  WoGroupApi extends  SuperApi<WoGroup> {

    @Autowired
    IWoTaskService iWoTaskService;

    @Autowired
    IWoGroupService iWoGroupService;

    @Autowired
    IWoDataService iWoDataService;


    @Autowired
    WoDataApi woDataApi;

    @ApiOperation("任务执行调度")
    @RequestMapping(value = "/dispatch",method = RequestMethod.POST)
    public DataVo dispatch(@RequestBody TaskDispatchPo po){
        if(ReflectionUtil.isNull(po.getGroupId())){
            ApiException.throw400("任务组编号为空");
        }

        if(po.getStatus() == null || po.getStatus().isEmpty()){
            //等待，错误的重试
            po.setStatus(Arrays.asList( WoTaskConstant.status.await,WoTaskConstant.status.error));
        }

        long total =  iWoTaskService.syncDispatch(po);
        DataVo vo =  DataVo.of();
        vo.setMessage("任务组开始调度,共"+total+"个任务执行");


        return vo;
    }


    @ApiOperation("导出任务组数据")
    @RequestMapping(value = "/exportData",method = RequestMethod.GET)
    public void exportData( ExportDataPo po){
        IdsPo p =   new IdsPo(po.getIds());
        if(po.getDataType().equals("excel")){
            exportDataToExcel(p);
        }else if(po.getDataType().equals("json")){
            exportDataToJson(p);
        }else{
            ApiException.throw400("错误数据格式"+po.getDataType());
        }

    }

    @ApiOperation("导出任务组数据为json")
    @RequestMapping(value = "/exportDataToJson",method = RequestMethod.GET)
    public void exportDataToJson( IdsPo po){
         List<String> ids =  iWoGroupService.all(po).stream().flatMap(val -> {
             String[] strings = StringUtil.removeLine(val.getContent()).split(",");
             return Arrays.asList(strings).stream();
         }).map(val -> UuidUtil.getId(val)).collect(Collectors.toList());

         List<WoData> list =  iWoDataService.listByIds(ids);
         woDataApi.exportDataToJson(list);
    }

    @ApiOperation("导出任务组数据为excel")
    @RequestMapping(value = "/exportDataToExcel",method = RequestMethod.GET)
    public void exportDataToExcel( IdsPo po){
        List<String> ids =  iWoGroupService.all(po).stream().flatMap(val -> {
            String[] strings = StringUtil.removeLine(val.getContent()).split(",");
            return Arrays.asList(strings).stream();
        }).map(val -> UuidUtil.getId(val)).collect(Collectors.toList());
        List<WoData> list =  iWoDataService.listByIds(ids);
        woDataApi.exportDataToExcel(list);
    }
}
